/**
 * $Id: SMGPDeliverMessage.java,v 1.2 2006/09/15 01:59:34 martin Exp $
 *
 * Copyright 2005 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.smgpmsg;
import java.util.Calendar;
import java.util.Date;

import com.haoxi.gateway.util.TypeConvert;

public abstract class SMGPDeliverMessage extends SMGPMessage {
    protected int deliverType;

    public SMGPDeliverMessage(byte buf[]) throws IllegalArgumentException {
        int len = 81 + (buf[72] & 0xff);
        if (buf.length != len) {
            throw new IllegalArgumentException(
                    "The message that is sent by SMC is wrong");
        } else {
            super.buf = new byte[len];
            System.arraycopy(buf, 0, super.buf, 0, buf.length);
            sequence_Id = TypeConvert.byte2int(super.buf, 0);
            deliverType = buf[14];
            return;
        }
    }

    public byte[] getMsgId() {
        byte msgId[] = new byte[10];
        System.arraycopy(buf, 4, msgId, 0, 10);
        return msgId;
    }
    public String getMsgIdStr() {
        return SMGPSequenceSower.getTeleComMsgId(getStatusMsgId());
    }

    public int getIsReport() {

        return buf[14];
    }

    public int getMsgFormat() {
        return buf[15];
    }

    public Date getRecvTime() {
        Date date;
        try {
            byte tmpbyte[] = new byte[4];
            System.arraycopy(buf, 16, tmpbyte, 0, 4);
            String tmpstr = new String(tmpbyte);
            int tmpYear = Integer.parseInt(tmpstr);
            tmpbyte = new byte[2];
            System.arraycopy(buf, 20, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMonth = Integer.parseInt(tmpstr) - 1;
            System.arraycopy(buf, 22, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpDay = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 24, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpHour = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 26, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpMinute = Integer.parseInt(tmpstr);
            System.arraycopy(buf, 28, tmpbyte, 0, 2);
            tmpstr = new String(tmpbyte);
            int tmpSecond = Integer.parseInt(tmpstr);
            Calendar calendar = Calendar.getInstance();
            calendar.set(tmpYear, tmpMonth, tmpDay, tmpHour, tmpMinute,
                    tmpSecond);
            Date date1 = calendar.getTime();
            return date1;
        } catch (Exception e) {
            date = null;
        }
        return date;
    }

    public String getSrcTermID() {
        byte srcTermId[] = new byte[21];
        System.arraycopy(buf, 30, srcTermId, 0, 21);
        return (new String(srcTermId)).trim();
    }

    public String getDestTermID() {
        byte destTermId[] = new byte[21];
        System.arraycopy(buf, 51, destTermId, 0, 21);
        return (new String(destTermId)).trim();
    }

    public int getMsgLength() {
        return buf[72] & 0xff;
    }

    public byte[] getMsgContent() {
        if (0 == deliverType) {
            int len = buf[72] & 0xff;
            byte content[] = new byte[len];
            System.arraycopy(buf, 73, content, 0, len);
            return content;
        } else {
            return null;
        }
    }

    public String getReserve() {
        int loc = 73 + (buf[72] & 0xff);
        byte reserve[] = new byte[8];
        System.arraycopy(buf, loc, reserve, 0, 8);
        return (new String(reserve)).trim();
    }

    // 以下是状态报告的处理
    // 状态报告对应原消息的msgid
    public abstract byte[] getStatusMsgId() ;
    public abstract String getStatusMsgIdStr();
    public abstract Date getSubmitTime();
    public abstract Date getDoneTime() ;
    public abstract String getStat() ;
    public abstract String getErr() ;

    public abstract String toString() ;
    public int getRequestId() {
        return SMGPCommon.SMGP_DELIVER;
    }
}
